package com.zyd.dao;

import com.zyd.controller.vehicle.VehicleController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class CarLockDao {

    private static final Logger LOGGER = LoggerFactory.getLogger(CarLockDao.class);


    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 删除锁车数据
     * @param tagcode
     */
    public void deletByTagcode(String tagcode) {
        jdbcTemplate.update("UPDATE zydzn_car_lock SET dr = 1 , dr_time=? WHERE tagcode = ?", new Date(),tagcode);
    }

    /**
     * 像车锁表插入一条数据
     * @param tagcode
     * @param baseids
     */
    public void insert(String tagcode, String baseids) {
        jdbcTemplate.update("INSERT INTO zydzn_car_lock (tagcode,stations,start_time) VALUES ( ?, ?, ? ) ;", tagcode,baseids,new Date());
    }

    /**
     *  查询出当已锁前车辆对应的基站
     * @param tagcode
     */
    public String findStationsByTagcode(String tagcode) {
        String Station = null;
        try {
            Station = jdbcTemplate.queryForObject("SELECT stations FROM zydzn_car_lock WHERE dr=0 AND tagcode=? LIMIT 1 ", String.class, tagcode);
        } catch (DataAccessException e) {
            LOGGER.error("通过tagcode:["+tagcode+"],未查询到在zydzn_car_lock中的数据" );
        }
        return Station;
    }

}
